﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Text;
using System.Data;

namespace IHISCETeim.Web.MachineSys.FXModel
{
    public partial class AreaManagerFXFinanceLeaseMasterList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "AreaManagerFXFinanceLeaseMasterList";
            }


        }
        #region 属性 (查询条件)

        protected string GetonlyshowExpiryPeriods()
        {
            string optionhtml = "<option value=\"{0}\" {1}>{0}期</option>";
            string rhtml = string.Format("<option value=\"{0}\" {1}>{2}</option>", 0, onlyshowExpiryPeriods == 0 ? "selected" : "", "请选择....");
            for (int i = 1; i <= 8; i++)
            {
                rhtml += string.Format(optionhtml, i, i == onlyshowExpiryPeriods ? "selected" : "");
            }

            rhtml += string.Format("<option value=\"{0}\" {1}>{2}</option>", 30, onlyshowExpiryPeriods == 30 ? "selected" : "", "三期以上");
            return rhtml;
        }

        protected int onlyshowExpiryPeriods
        {
            get
            {
                return GetRequestValue<int>("onlyshowExpiryPeriods", 0);
            }
        }
        protected string AliasName
        {
            get
            {
                return GetRequestValue<string>("AliasName", "");

            }
        }
        protected string CustomerName
        {
            get
            {
                return GetRequestValue<string>("CustomerName", "");

            }
        }
        protected string LeaseCompany
        {
            get
            {
                return GetRequestValue<string>("LeaseCompany", "");
            }
        }

        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");
            }
        }
        protected string ContractNum
        {
            get
            {
                return GetRequestValue<string>("ContractNum", "");

            }
        }
        protected string FnncLsId
        {
            get
            {
                return GetRequestValue<string>("FnncLsId", "");

            }
        }

        protected string LoansDateFrom
        {
            get
            {
                return GetRequestValue<string>("LoansDateFrom", "");
            }
        }

        protected string LoansDateTo
        {
            get
            {
                return GetRequestValue<string>("LoansDateTo", LoansDateFrom);
            }
        }

        protected int LeaseStatus
        {
            get
            {
                return GetRequestValue<int>("LeaseStatus", -1);

            }
        }



        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "AliasName" : base.orderField;
            }
        }
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "ASC" : base.orderDirection;
            }
        }
        protected new string SQLSort
        {
            get
            {
                return (orderField + " " + orderDirection) + (LeaseStatus == -2 ? ",SerialNumber desc" : " ,FnncLsId desc ");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
            this.AddLog(Permission.查看, GetWhereSql());
        }
        protected string GetWhereSql()
        {
            string agentsqltemp = SAreaManagerAgentController.GetMyAgentSqlWhere();
            if (string.IsNullOrEmpty(agentsqltemp))
                return "1<>1";
            string wheresql = string.Format(" AliasName in ({0})", agentsqltemp);


            if (LeaseStatus > -1)
            {
                string LeaseStatussql = QueryOperatorsEnumHelper.ConvertCommonOperator("LeaseStatus", QueryOperatorsEnum.等于, LeaseStatus.ToString());
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = LeaseStatussql;
                else
                    wheresql += " and " + LeaseStatussql;
            }
            else
            {
                string LeaseStatussql = string.Format("LeaseStatus in ({0})", "0,1,2,3");
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = LeaseStatussql;
                else
                    wheresql += " and " + LeaseStatussql;
            }

            if (!AliasName.IsEmptyOrNull())
            {
                string aliasNamesql = QueryOperatorsEnumHelper.ConvertCommonOperator("AliasName", QueryOperatorsEnum.等于, AliasName);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = aliasNamesql;
                else
                    wheresql += " and " + aliasNamesql;
            }

            if (!LeaseCompany.IsEmptyOrNull())
            {
                string aliasNamesql = QueryOperatorsEnumHelper.ConvertCommonOperator("LeaseCompany", QueryOperatorsEnum.等于, LeaseCompany);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = aliasNamesql;
                else
                    wheresql += " and " + aliasNamesql;
            }

            if (!CustomerName.IsEmptyOrNull())
            {
                string CustomerNamesql = QueryOperatorsEnumHelper.ConvertCommonOperator("CustomerName", QueryOperatorsEnum.包含, CustomerName);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = CustomerNamesql;
                else
                    wheresql += " and " + CustomerNamesql;
            }


            if (!SerialNumber.IsEmptyOrNull())
            {
                string SerialNumbersql = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = SerialNumbersql;
                else
                    wheresql += " and " + SerialNumbersql;
            }

            if (!ContractNum.IsEmptyOrNull())
            {
                string ContractNumsql = QueryOperatorsEnumHelper.ConvertCommonOperator("ContractNum", QueryOperatorsEnum.包含, ContractNum);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = ContractNumsql;
                else
                    wheresql += " and " + ContractNumsql;
            }

            if (!FnncLsId.IsEmptyOrNull())
            {
                string FnncLsIdsql = QueryOperatorsEnumHelper.ConvertCommonOperator("FnncLsId", QueryOperatorsEnum.等于, FnncLsId);
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = FnncLsIdsql;
                else
                    wheresql += " and " + FnncLsIdsql;
            }

            if (onlyshowExpiryPeriods > 0)
            {
                string FnncLsIdsql = onlyshowExpiryPeriods == 30 ? QueryOperatorsEnumHelper.ConvertCommonOperator("LExpiryPeriods", QueryOperatorsEnum.大于等于, "3") : QueryOperatorsEnumHelper.ConvertCommonOperator("LExpiryPeriods", QueryOperatorsEnum.等于, onlyshowExpiryPeriods.ToString());
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = FnncLsIdsql;
                else
                    wheresql += " and " + FnncLsIdsql;
            }

            if (!string.IsNullOrEmpty(LoansDateFrom))
            {
                string LoansDateFromsql = QueryOperatorsEnumHelper.ConvertCommonOperator("LoansDate", QueryOperatorsEnum.范围, LoansDateFrom + " 00:00:00|" + LoansDateTo + " 23:59:59");
                if (string.IsNullOrEmpty(wheresql))
                    wheresql = LoansDateFromsql;
                else
                    wheresql += " and " + LoansDateFromsql;
            }

            return wheresql;
        }
        protected DataTable sumdt = null;
        private void DataBind()
        {
            DataTableCollection dtc = null;
            //  sumdt = dtc[1];
            switch (LeaseStatus)
            {
                case -2:
                     string agentsqltemp = SAreaManagerAgentController.GetMyAgentSqlWhere();
                     string wheresql = "";
                    if (!string.IsNullOrEmpty(agentsqltemp))
                        wheresql = string.Format(" AliasName in ({0})", agentsqltemp);
                    dtc = FXFinanceLeaseController.FXFinanceLeaseWSBGetPagination(wheresql, pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu000.DataSource = dtc[0];
                    this.rpMenu000.DataBind();
                    break;
                case -1:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu00.DataSource = dtc[0];
                    this.rpMenu00.DataBind();
                    break;
                case 0:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu0.DataSource = dtc[0];
                    this.rpMenu0.DataBind();
                    break;
                case 1:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu1.DataSource = dtc[0];
                    this.rpMenu1.DataBind();
                    break;
                case 2:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu2.DataSource = dtc[0];
                    this.rpMenu2.DataBind();
                    break;
                case 3:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu3.DataSource = dtc[0];
                    this.rpMenu3.DataBind();
                    break;
                case 4:
                    dtc = FXFinanceLeaseController.GetPagination(GetWhereSql(), pageNum, numPerPage, SQLSort, ref TotalCount);
                    sumdt = dtc[1];
                    this.rpMenu4.DataSource = dtc[0];
                    this.rpMenu4.DataBind();
                    break;
            }



        }


        #region 状态跳转

        protected string GetGoLeaseStatusUrl(object _FnncLsId, object _LeaseStatus)
        {
            string LeaseStatusText = GetLeaseStatusText(_LeaseStatus);
            int LeaseStatus = Convert.ToInt16(_LeaseStatus);
            string NextLeaseStatusText = GetLeaseStatusText(LeaseStatus + 1);
            // string goUrl = "<b style=\"color:{0};\">{1}</b><a  onclick='return false;' href=\"FXModel/FXFinanceLeaseUpdate.aspx?FnncLsId={3}&NextLeaseStatus={4}\" target=\"ajax\" rel=\"FXFinanceLeaseAddEdit\" alt=\"进入{2}\">=></a>";
            string goUrl = "<b style=\"color:{0};\">{1}</b>";
            string colorHtml = LeaseStatus == 1 ? "red" : LeaseStatus == 2 ? "GREEN" : LeaseStatus == 3 ? "#E78629" : "";
            return string.Format(goUrl, colorHtml, LeaseStatusText);
            // return string.Format(goUrl, colorHtml, LeaseStatusText, NextLeaseStatusText, _FnncLsId, LeaseStatus >= 4 ? 0 : LeaseStatus+1);
        }
        #endregion
        /// <summary>
        /// 获取各状态的总数
        /// </summary>
        /// <param name="lease">状态名称 : 审核中 ,审核通过,待放款,已放款</param>
        /// <returns></returns>
        protected string GetLeaseStatusCount(string lease)
        {
            try
            {
                DataRow dr = leaseStatusDT.Rows[0];
                return dr[lease].ToString();
            }
            catch
            {
                return "-";
            }
        }

        private DataTable _leaseStatusDT = null;
        private DataTable leaseStatusDT
        {
            get
            {
                if (_leaseStatusDT == null)
                {
                    string agentsqltemp = SAreaManagerAgentController.GetMyAgentSqlWhere();
                    if (string.IsNullOrEmpty(agentsqltemp))
                         agentsqltemp="'none'";
                    _leaseStatusDT = FXFinanceLeaseController.GetLeaseStatusCount(agentsqltemp);
                }
                return _leaseStatusDT;
            }
        }


        protected string GetDateShowType(object datevalue)
        {
            if (datevalue == DBNull.Value)
                return "";
            DateTime dtemp = Convert.ToDateTime(datevalue);
            return dtemp.ToString("yyyy-MM-dd");
        }

        protected string getsum(string columnname)
        {
            if (sumdt == null)
                return "0";
            try
            {
                return Convert.ToDecimal(sumdt.Rows[0][columnname]).ToString("0,0.00");
            }
            catch
            {
                return "0";
            }
        }

        protected string getCleanUpStatus(object _CleanUpStatus)
        {
            short CleanUpStatus = Convert.ToInt16(_CleanUpStatus);
            return CleanUpStatus == -1 ? "<b style='color:red'>未清<b>" : CleanUpStatus == 0 ? "<b style='color:red'>已到期未结清</b>" : "已结清";
        }
        /// <summary>
        /// 投保情况 1已投保,0未投保
        /// </summary>
        /// <param name="_OInsureStatus"></param>
        /// <returns></returns>
        protected string getOInsureStatus(object _OInsureStatus)
        {
            short OInsureStatus = Convert.ToInt16(_OInsureStatus);
            return OInsureStatus == 0 ? "<b style='color:red'>未投保<b>" : "已投保";
        }
        /// <summary>
        /// 1停止服务(已到期的情况) .0正常
        /// </summary>
        /// <param name="_GPSStatus"></param>
        /// <returns></returns>
        protected string getGPSStatus(object _GPSStatus)
        {
            short GPSStatus = Convert.ToInt16(_GPSStatus);
            return GPSStatus == 1 ? "<b >停止服务<b>" : "服务中";
        }
        /// <summary>
        /// 资料、协议收集是否完成
        /// 0未完成，1已完成,2只承诺函，3缺承诺函
        /// </summary>
        /// <param name="_MaterialStatus"></param>
        /// <returns></returns>
        protected string getMaterialStatus(object _MaterialStatus)
        {
            short MaterialStatus = Convert.ToInt16(_MaterialStatus);
            switch (MaterialStatus)
            {
                case 0:
                    return "<b style='color:red'>未完成<b>";
                    break;
                case 1:
                    return "已完成";
                    break;
                case 2:
                    return "只承诺函";
                    break;
                case 3:
                    return "缺承诺函";
                    break;

            }
            return "";
        }
        /// <summary>
        /// 管理费支付状态 
        /// 1已付,0未付
        /// </summary>
        /// <param name="_LMFeeStatus"></param>
        /// <returns></returns>
        protected string getLMFeeStatus(object _LMFeeStatus)
        {
            short LMFeeStatus = Convert.ToInt16(_LMFeeStatus);
            return LMFeeStatus == 0 ? "<b style='color:red'>未付<b>" : "已付";
        }

        /// <summary>
        /// 应付首付款支付状态 
        /// 1已付,0未付,2无需支付(小于等0）
        /// </summary>
        /// <param name="_LMFeeStatus"></param>
        /// <returns></returns>
        protected string getLPayStatus(object _LPayStatus)
        {
            short LPayStatus = Convert.ToInt16(_LPayStatus);
            return LPayStatus == 0 ? "<b style='color:red'>未付<b>" : LPayStatus == 1 ? "已付" : "无需支付";
        }

        protected string GetMyAgentList(string selectName, string selectValue)
        {
            List<SAreaManagerAgentInfo> listArea = SAreaManagerAgentController.GetAllByUserId(base.User.UserId);
            string html = "<select name='{0}' class='combox {2}'>{1}</select>";
            string optiontemp = "";
           // string[] lcList = new string[] { "三井住友", "兴银融资", "东瑞盛世利" };
            optiontemp += string.Format("<option value='{0}' {2}>{1}</option>", "", "请选择...", selectValue == "" ? "selected" : "");
            foreach (SAreaManagerAgentInfo itm in listArea)
            {
                optiontemp += string.Format("<option value='{0}' {2}>{1}</option>", itm.AliasName, itm.AliasName, selectValue == itm.AliasName ? "selected" : "");
            }

            return string.Format(html, selectName, optiontemp, "");
        }
    }
}